<html>
<head><meta charset="utf-8"><title>Dynamic behaviour based on abstract type parameter · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dynamic.20behaviour.20based.20on.20abstract.20type.20parameter.html">Dynamic behaviour based on abstract type parameter</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="220711251"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dynamic%20behaviour%20based%20on%20abstract%20type%20parameter/near/220711251" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David McGillicuddy <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dynamic.20behaviour.20based.20on.20abstract.20type.20parameter.html#220711251">(Dec 22 2020 at 16:59)</a>:</h4>
<p>Hi, I appear to have abstracted myself into a corner.<br>
I have some struct S with some type parameter T like: <code>struct S&lt;T&gt; {t: T}</code> where <code>T</code> is some combination of traits including the serde ones, sized, debug, etc. Whatever I found myself needing.<br>
However I find myself wanting to have an if-statement based on what <code>T</code> is to give it dynamic runtime behaviour. What are my options here? I could e.g. make a trait that reifies into an enum of the allowed <code>T</code> types? Just implement the runtime behaviour (i.e. the if-branch bodies) as a method in this new trait directly? I'm slowly inflating the number of trait bounds on <code>T</code> it's already up to _twelve_.<br>
What kind of advice do people have for these situations?</p>



<a name="220711443"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dynamic%20behaviour%20based%20on%20abstract%20type%20parameter/near/220711443" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dynamic.20behaviour.20based.20on.20abstract.20type.20parameter.html#220711443">(Dec 22 2020 at 17:01)</a>:</h4>
<p>Do you know all the allowed types for <code>T</code> beforehand? In that case, maybe it is easier to work with an enum of all allowed types in general, instead of making <code>S</code> generic.</p>



<a name="220711768"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dynamic%20behaviour%20based%20on%20abstract%20type%20parameter/near/220711768" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David McGillicuddy <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dynamic.20behaviour.20based.20on.20abstract.20type.20parameter.html#220711768">(Dec 22 2020 at 17:05)</a>:</h4>
<p>I do know all the allowed types for <code>T</code> before hand yes. At this point I can't even remember why I decided to make it an abstract type in the first place but it is nice being able to pass it around fearlessly i.e. compile time guarentees about which variant you have. I guess this is the tradeoff! Maybe once Rust gets dependant types I can get the best of both worlds, I'll get back to you in a decade...</p>
<p>edit: or maybe something like <code>syn</code>'s design where you have enums each containing bespoke structs? Feels like a real deep rabbit hole.</p>



<a name="220713960"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dynamic%20behaviour%20based%20on%20abstract%20type%20parameter/near/220713960" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dynamic.20behaviour.20based.20on.20abstract.20type.20parameter.html#220713960">(Dec 22 2020 at 17:29)</a>:</h4>
<p>if it's 'static and you can ask for that in bound, you can dynamically downcast via casting to Any as long as you don't need ownership safely, and with a bit of unsafe code unsafely (but soundly) if you do need ownership</p>



<a name="220714068"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dynamic%20behaviour%20based%20on%20abstract%20type%20parameter/near/220714068" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David McGillicuddy <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dynamic.20behaviour.20based.20on.20abstract.20type.20parameter.html#220714068">(Dec 22 2020 at 17:30)</a>:</h4>
<p>Is this recommended or at least fairly normal practice, or more of a nuclear option? I've never used <code>Any</code> before.</p>



<a name="220714337"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dynamic%20behaviour%20based%20on%20abstract%20type%20parameter/near/220714337" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dynamic.20behaviour.20based.20on.20abstract.20type.20parameter.html#220714337">(Dec 22 2020 at 17:32)</a>:</h4>
<p>If I have 'static it's probably what I'd reach for first if I have just one edge case but if there's going to be many then I'd add a trait and dispatch that way</p>



<a name="220714381"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dynamic%20behaviour%20based%20on%20abstract%20type%20parameter/near/220714381" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dynamic.20behaviour.20based.20on.20abstract.20type.20parameter.html#220714381">(Dec 22 2020 at 17:33)</a>:</h4>
<p>I pretty rarely have 'static though :)</p>



<a name="220717663"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dynamic%20behaviour%20based%20on%20abstract%20type%20parameter/near/220717663" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David McGillicuddy <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dynamic.20behaviour.20based.20on.20abstract.20type.20parameter.html#220717663">(Dec 22 2020 at 18:05)</a>:</h4>
<p>Thanks both for the suggestions, I'll have to think about it for sure. This is the first rust codebase I've worked in that ended up in the classic state of being worked on by a half dozen people, some of whom have been in a hurry, who aren't all perfectly aligned on idioms, with new requirements dropping in every month or so, etc, and I think that's part of the pain that I'm currently feeling.</p>



<a name="220718218"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dynamic%20behaviour%20based%20on%20abstract%20type%20parameter/near/220718218" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dynamic.20behaviour.20based.20on.20abstract.20type.20parameter.html#220718218">(Dec 22 2020 at 18:11)</a>:</h4>
<blockquote>
<p>I'm slowly inflating the number of trait bounds on T it's already up to _twelve_.</p>
</blockquote>
<p>I'd probably make an almost-trait-alias like</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">Concept</span>: <span class="nc">Twelve</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">Traits</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">Here</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">new_thing</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>And then implement it for the types in question.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>